﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

using BEX.Core;

using BEX.Core.Exchange;
using BEX.Core.Exchange.BITFINEX;
using BEX.Core.Exchange.BITSTAMP;
using BEX.Core.Exchange.BTCE;
using BEX.Core.Exchange.CAMPBX;
using BEX.Core.Exchange.Coinbase;
using BEX.Core.Exchange.MtGox;
using BEX.Core.Exchange.TRADEHILL;


namespace BEX.DataAccess
{
    public class ExchangeDAL
    {
        public static Exchange GetByID(int id)
        {

            var _context = new BTCDataClassesDataContext();


            var _r = (from acc in _context.GetTable<EXCHANGE>()
                      where (acc.ID == id)
                      select acc).ToList<EXCHANGE>();


            Exchange res = null;

            if (null != _r && _r.Count > 0)
            {

                switch ((ExchangeType)id)
                {
                    case (ExchangeType.BITFINEX):
                        res = new Bitfinex();
                        break;
                    case (ExchangeType.BITSTAMP):
                        res = new Bitstamp();
                        break;
                    case (ExchangeType.BTCE):
                        res = new BTCE();
                        break;
                    case (ExchangeType.CAMPBX):
                        res = new CampBX();
                        break;
                    case (ExchangeType.COINBASE):
                        res = new Coinbase();
                        break;
                    case (ExchangeType.MTGOX):
                        res = new MtGox();
                        break;
                    case (ExchangeType.TRADEHILL):
                        res = new Tradehill();
                        break;
                    default:
                        throw new Exception("Unable to determine Exchange type in ExchangeDAL");
                }

                res.ID = _r[0].ID;
                res.Name = _r[0].NAME;
                res.QueryFrequency = _r[0].QUERY_FREQUENCY;
                //res.TickerURL = _r[0].TICKER_URL;

                var _pairs = (from acc in _context.GetTable<EXCHANGE_CURRENCY>()
                              where (acc.EXCHANGE_ID == res.ID)
                              select acc).ToList<EXCHANGE_CURRENCY>();

                /*if (null != _pairs && _pairs.Count > 0)
                {
                    for (int i = 0; i < _pairs.Count; ++i)
                    {
                        CurrencyPair _new = new CurrencyPair();
                        _new.Base = (CurrencyType)_pairs[i].BASE_CURRENCY_ID;
                        _new.Counter = (CurrencyType)_pairs[i].COUNTER_CURRENCY_ID;

                        res.CurrencyPairs.Add(_new);

                    }

                }*/
            }


            return res;
        }

        public static CurrencyType[] GetSupportedBaseCurrencies(ExchangeType type)
        {

            ArrayList buf = new ArrayList();

            BTCDataClassesDataContext _context = new BTCDataClassesDataContext();

            List<EXCHANGE_CURRENCY> _e = (from acc in _context.GetTable<EXCHANGE_CURRENCY>()
                                          where (acc.EXCHANGE_ID == (int)(type))
                                          select acc).ToList<EXCHANGE_CURRENCY>();


            if (null != _e && _e.Count > 0)
            {
                for (int i = 0; i < _e.Count; ++i)
                {
                    if (!buf.Contains(_e[i].BASE_CURRENCY_ID))
                        buf.Add(_e[i].BASE_CURRENCY_ID);

                }

            }

            CurrencyType[] res = new CurrencyType[buf.Count];

            int x = 0;
            foreach (int _t in buf)
            {
                res[x] = (CurrencyType)(_t);
                ++x;
            }

            return res;

        }

        public static CurrencyType[] GetSupportedCounterCurrencies(ExchangeType type, CurrencyType basec)
        {

            ArrayList buf = new ArrayList();

            BTCDataClassesDataContext _context = new BTCDataClassesDataContext();

            List<EXCHANGE_CURRENCY> _e = (from acc in _context.GetTable<EXCHANGE_CURRENCY>()
                                          where (acc.EXCHANGE_ID == (int)(type) && acc.BASE_CURRENCY_ID == (int)(basec))
                                          select acc).ToList<EXCHANGE_CURRENCY>();


            if (null != _e && _e.Count > 0)
            {
                for (int i = 0; i < _e.Count; ++i)
                {
                    if (!buf.Contains(_e[i].COUNTER_CURRENCY_ID))
                        buf.Add(_e[i].COUNTER_CURRENCY_ID);

                }

            }

            CurrencyType[] res = new CurrencyType[buf.Count];

            int x = 0;
            foreach (int _t in buf)
            {
                res[x] = (CurrencyType)(_t);
                ++x;
            }

            return res;

        }

        public static List<CurrencyType> GetAllBaseCurrencies()
        {
            List<CurrencyType> res = new List<CurrencyType>();

            BTCDataClassesDataContext _context = new BTCDataClassesDataContext();

            List<int> _e = (from acc in _context.GetTable<EXCHANGE_CURRENCY>()
                                          select acc.BASE_CURRENCY_ID).Distinct().ToList();


            if (null != _e && _e.Count > 0)
            {
                for (int i = 0; i < _e.Count; ++i)
                {
                    res.Add((CurrencyType)_e[i]);
                }
            }

          
            return res;
        }

        public static List<CurrencyType> GetAllCounterCurrencies()
        {



            List<CurrencyType> res = new List<CurrencyType>();

            BTCDataClassesDataContext _context = new BTCDataClassesDataContext();

            List<int> _e = (from acc in _context.GetTable<EXCHANGE_CURRENCY>()
                            select acc.COUNTER_CURRENCY_ID).Distinct().ToList();


            if (null != _e && _e.Count > 0)
            {
                for (int i = 0; i < _e.Count; ++i)
                {
                    res.Add((CurrencyType)_e[i]);
                }
            }


            return res;
        }
    }
}
